package fr.upstlse.glre.client.secretaire.actualite;

import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.Window;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.button.ToolButton;
import com.extjs.gxt.ui.client.widget.form.FormPanel;

import fr.upstlse.glre.client.RfgConstante;
import fr.upstlse.glre.client.constante.RfgConstanteEnum;
import fr.upstlse.glre.client.constante.fields.ActualiteConstante;
import fr.upstlse.glre.client.secretaire.actualite.action.DeleteActualiteAction;
import fr.upstlse.glre.client.secretaire.actualite.action.ModifyActualiteAction;
import fr.upstlse.glre.client.vo.actualite.ActualiteVOGWT;
import fr.upstlse.glre.client.widgets.fields.ActualiteFields;

public class ModificationActualiteWindow extends Window {
	private ActualiteVOGWT actualiteVOGWT;
	private boolean editionMode;

	private FormPanel formPanel;
	private ActualiteFields actualiteFields;

	private static ModificationActualiteWindow instance;

	protected ModificationActualiteWindow(ActualiteVOGWT pActualiteVOGWT) {
		super();
		actualiteVOGWT = pActualiteVOGWT;
		setId(ActualiteConstante.WINDOW_ID.getValue());
		setHeading(ActualiteConstante.WINDOW_HEADER_TEXT.getValue());

		initWidget();
	}

	private void initWidget() {
		editionMode = false;
		readMode();
		// Tool Button
		ToolButton lSwithToEditionMode = new ToolButton(".my-tool-gear");
		lSwithToEditionMode.setStyleAttribute("background",
				" transparent url(images/edit.png) no-repeat");
		lSwithToEditionMode
				.setToolTip(RfgConstanteEnum.MODIFICATION_SWITCH_BUTTON_TOOLTIP
						.getValue()
						+ " l'actualité");
		lSwithToEditionMode
				.addSelectionListener(new SelectionListener<ComponentEvent>() {
					public void componentSelected(ComponentEvent ce) {
						if (!editionMode) {
							removeAll();
							editionMode();
							editionMode = true;
							layout();
						} else {
							removeAll();
							readMode();
							editionMode = false;
							layout();
						}
					}
				});
		getHeader().addTool(lSwithToEditionMode);
		createDeleteButton();
	}

	private void createDeleteButton() {
		ToolButton lDeleteButton = new ToolButton(".my-tool-gear");
		lDeleteButton.setStyleAttribute("background",
				" transparent url(images/supprimer.png) no-repeat");
		lDeleteButton.setToolTip(RfgConstante.DELETE_BUTTON_TOOLTIP
				+ " l'actualité");
		lDeleteButton
				.addSelectionListener(new SelectionListener<ComponentEvent>() {
					public void componentSelected(ComponentEvent ce) {
						MessageBox.confirm("Confirmation",
								"Voulez-vous supprimer l'actualité "
										+ actualiteVOGWT.getTitre() + " ?",
								new DeleteActualiteAction(actualiteVOGWT));
					}
				});
		getHeader().addTool(lDeleteButton);
	}

	private void readMode() {
		add(actualiteVOGWT.getHtml());
	}

	private void editionMode() {
		// formulaire sans action
		formPanel = new FormPanel();
		formPanel.setHeaderVisible(false);
		formPanel.setAutoWidth(true);

		actualiteFields = new ActualiteFields(actualiteVOGWT, formPanel);
		Button lSubmit = new Button(
				RfgConstante.PROFIL_EDITING_FIELD_SUBMIT_LABEL);
		lSubmit.addSelectionListener(new ModifyActualiteAction(actualiteFields));
		formPanel.addButton(lSubmit);
		add(formPanel);
	}

	public static ModificationActualiteWindow getInstance(
			ActualiteVOGWT pActualiteVOGWT) {
		if (instance == null) {
			instance = new ModificationActualiteWindow(pActualiteVOGWT);
		}

		return instance;
	}

	public static void closeCurrentWindow() {
		if (instance != null) {
			instance.hide();
			instance = null;
		}
	}
}
